<?php
/**
 * Created by PhpStorm.
 * User: dungnt
 * Date: 12/3/14
 * Time: 10:00 PM
 */

namespace App\Services;

use Config;
use Lang;
use Sentry;
use Comment;
use Like;
use Lesson;
use Video;
use DB;
use Cache;
use Redis;
use Exception;
use Paginator;

class ActivityServices extends BaseService
{
    protected static $redis;


    public function addComment($data)
    {
        $saveId = $this->saveItem("Comment", $data);
//        $redis = $this->redis();
//        $redis->zadd('commentRef', 2, 2);
//        $redis = self::redis();

    }

    public function addVote($data)
    {
        $params = array();
        $params['id'] = $data['id'];
        $params['user_id'] = $data['user_id'];
        $params['field'] = 'votes';
        $params['remove'] = $data['remove'];
        $params['function'] = 'votes';
        if ($data['class'] == 'lesson') {
            $params['Class'] = 'Lesson';
        } else if ($data['class'] == 'answer') {
            $params['Class'] = 'Answer';
        } else if ($data['class'] == 'question') {
            $params['Class'] = 'Question';
        }
        $flag = $this->attach($params);
        return $flag;
    }

    public function addFavourite($data)
    {
        $params = array();
        $params['id'] = $data['id'];
        $params['user_id'] = $data['user_id'];
        $params['field'] = 'favourites';
        $params['remove'] = $data['remove'];
        $params['function'] = 'favourites';
        if ($data['class'] == 'lesson') {
            $params['Class'] = 'Lesson';
        } else if ($data['class'] == 'answer') {
            $params['Class'] = 'Answer';
        } else if ($data['class'] == 'question') {
            $params['Class'] = 'Question';
        } else if ($data['class'] == 'video') {
            $params['Class'] = 'Video';
        }
        $flag = $this->attach($params);
        return $flag;
    }

    public function topComments($data)
    {
        $field = 'lesson_id';
        switch ($data['type']) {
            case 'lesson':
                $field = 'lesson_id';
                break;
            case 'video':
                $field = 'video_id';
                break;
            case 'lesson_id';
                $field = 'video_id';
                break;
        }
        $key = 'top_comments_on' . $data['type'];
        $comments = array();
        if (Cache::has($key))
            $comments = Cache::get($key);
        else {
            $class = Comment::join('users', 'users.id', '=', 'comments.user_id');
            if ($data['type'] == 'lesson')
                $class = $class->join('lessons', 'lessons.id', '=', 'comments.lesson_id')->where('comments.lesson_id', '!=', 0);
            else if ($data['type'] == 'video')
                $class = $class->join('videos', 'videos.id', '=', 'comments.video_id')->where('comments.video_id', '!=', 0);
            $comments = $class->select('comments.*', 'username', 'title', 'slug')
                ->orderBy('id', 'DESC')->where('parent_id', '=', 0)
                ->take(10)->get();
            Cache::put($key, $comments, 60);
        }
        return $comments;
    }

    public function listComment($data = array())
    {
        Paginator::setPageName('page_comment');
        $type = 'lesson_id';
        if ($data['target'] == 'video')
            $type = 'video_id';
        $comments = Comment::join('users', 'users.id', '=', 'comments.user_id')
            ->where('parent_id', '=', 0)->where('status', '=', 'on')->where($type, '=', $data['id'])
            ->select('comments.*', 'username', 'avatar')
            ->orderBy('id', 'DESC')
            ->paginate(15);
        return $comments;
    }

//    public function joinCouse
} 